﻿using GrapeCity.Forguncy.CellTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vant.CellTypes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// Interaction logic for HorizontalStepDrawingObject.xaml
    /// </summary>
    public partial class HorizontalStepDrawingObject : UserControl
    {
        public HorizontalStepDrawingObject(Steps steps, ICellInfo cell, IDrawingHelper drawingHelper)
        {
            var viewModel = new HorizontalStepDrawingObjectViewModel();
            var width = drawingHelper.GetImageCellSize().Width;
            width -= 20;
            var items = new List<Step>(steps.options);
            if (items.Count == 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    items.Add(new Step()
                    {
                        title = "步骤" + (i + 1),
                        value = (i + 1).ToString(),
                    });
                }
            }
            var itemWith = width / (items.Count - 1);

            var currentStepIndex = items.Select(i => i.value?.ToString()).ToList().IndexOf(cell.Value?.ToString());

            if(currentStepIndex == -1 && !string.IsNullOrEmpty(cell.Value?.ToString()))
            {
                currentStepIndex = items.Count;
            }

            for (int i = 0; i < items.Count; i++)
            {
                bool isFirst = i == 0;
                bool isLast = i == items.Count - 1;

                var item = items[i];
                var itemVM = new StepItemViewModel()
                {
                    NodeText = item.title,
                    Width = itemWith,
                    BorderMargin = new Thickness(0, 14, 0, 0)
                };
                var dotMarginTop = 12;
                var iconMarginTop = 8;
                if (isFirst)
                {
                    itemVM.StepDotMargin = new Thickness(2, dotMarginTop, 8, 0);
                    itemVM.StepIconMargin = new Thickness(2, iconMarginTop, 8, 0);
                    itemVM.LeftBorderVisibility = Visibility.Collapsed;
                    itemVM.LeftGridWidth = GridLength.Auto;
                    itemVM.Width = itemWith / 2;
                    itemVM.TextHorizontalAlignment = HorizontalAlignment.Left;
                }
                else if (isLast)
                {
                    itemVM.StepDotMargin = new Thickness(8, dotMarginTop, 2, 0);
                    itemVM.StepIconMargin = new Thickness(8, iconMarginTop, 2, 0);
                    itemVM.RightBorderVisibility = Visibility.Collapsed;
                    itemVM.RightBorderVisibility = Visibility.Collapsed;
                    itemVM.RightGridWidth = GridLength.Auto;
                    itemVM.Width = itemWith / 2;
                    itemVM.TextHorizontalAlignment = HorizontalAlignment.Right;
                }
                else
                {
                    itemVM.StepDotMargin = new Thickness(8, dotMarginTop, 8, 0);
                    itemVM.StepIconMargin = new Thickness(8, iconMarginTop, 8, 0);
                }

                if (i == currentStepIndex)
                {
                    itemVM.Icon = IconHelper.GetIcon(steps.activeIcon, drawingHelper) ?? IconHelper.GetBuiltInIcon("circleCheckFull", drawingHelper, steps.activeColor);
                    itemVM.TextColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.StepColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.LeftBorderColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.RightBorderColor = drawingHelper.GetBrush(steps.inactiveColor);
                }
                else if (i < currentStepIndex)
                {
                    itemVM.Icon = IconHelper.GetIcon(steps.finishIcon, drawingHelper);
                    itemVM.TextColor = drawingHelper.GetBrush("#323233");
                    itemVM.StepColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.LeftBorderColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.RightBorderColor = drawingHelper.GetBrush(steps.activeColor);
                }
                else
                {
                    itemVM.Icon = IconHelper.GetIcon(steps.inactiveIcon, drawingHelper);
                    itemVM.TextColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.StepColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.LeftBorderColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.RightBorderColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.TextColor = drawingHelper.GetBrush(steps.inactiveColor);
                }
                viewModel.Items.Add(itemVM);
            }

            this.DataContext = viewModel;
            InitializeComponent();
        }

        public class HorizontalStepDrawingObjectViewModel : ModelBase
        {
            public List<StepItemViewModel> Items { get; set; } = new List<StepItemViewModel>();
        }

        public class StepItemViewModel
        {
            public double Width { get; set; }
            public string NodeText { get; set; }
            public Brush TextColor { get; set; }
            public HorizontalAlignment TextHorizontalAlignment { get; set; } = HorizontalAlignment.Center;
            public Visibility LeftBorderVisibility { get; set; }
            public Thickness LeftBorderMargin { get; set; }
            public Brush LeftBorderColor { get; set; }
            public GridLength LeftGridWidth { get; set; } = new GridLength(1, GridUnitType.Star);

            public Visibility RightBorderVisibility { get; set; }
            public Thickness RightBorderMargin { get; set; }
            public Brush RightBorderColor { get; set; }
            public GridLength RightGridWidth { get; set; } = new GridLength(1, GridUnitType.Star);
            public object Icon { get; set; }
            public Thickness StepIconMargin { get; set; }
            public Thickness StepDotMargin { get; set; }
            public Thickness BorderMargin { get; set; }
            public Brush StepColor { get; set; }
            public Visibility StepIconVisibility { get { return Icon != null ? Visibility.Visible : Visibility.Collapsed; } }
            public Visibility StepDotVisibility { get { return Icon == null ? Visibility.Visible : Visibility.Collapsed; } }
        }
    }
}
